package anatex.service;

import java.util.Date;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import anatex.kea.BaseAlgo;
import anatex.kea.genex.GenEx;
import anatex.kea.genex.Genitor;
import anatex.kea.stemmers.Stemmer;

import anatex.kea.kea.Kea;

import anatex.domain.*;
import anatex.service.KeaSettings;
import anatex.service.Settings;
import anatex.translate.TranslateTask;

import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;
import anatex.kea.stemmers.*;

@Service
public class KeaDocumentDomainTask {

	private static Integer timeCounter = 0;
	
	@Scheduled(fixedRate=10000)
	@Async
	public void executeKea() {
		System.out.println("In execute KeaDocumentDomain task");
		
		Settings settings = Settings.getConfig();

		if(settings.getKeaServiceEnabled() == false){
			return;
		}else{
			if(KeaDocumentDomainTask.timeCounter < settings.getKeaTimeInterval()){
				KeaDocumentDomainTask.timeCounter++;
				return;
			}else{
				KeaDocumentDomainTask.timeCounter = 0;
			}
		}
		

		DocumentDomainTask task = null;
		try {
			task = DocumentDomainTask.findDocumentDomainTasksByCurrentStateAndEnabled(DocumentDomainTask.DomainTaskEnumState.NOT_STARTED,
					true).setFirstResult(0).setMaxResults(1).getSingleResult();
		}catch(EmptyResultDataAccessException ex){
			return;
		}catch (Exception ex) {
			System.out.println("KeaTask error");
			ex.printStackTrace(System.out);
		}
		System.out.println("KeaTask before if ");
		if (task != null) {
			
			task.setCurrentState(DocumentDomainTask.DomainTaskEnumState.WORKING);
			task.setStartTime(new Date());
			try{
				task = task.merge();
			}catch(Exception ex){
				System.out.println(" merge etc 1");
				ex.printStackTrace();
			}
			System.out.println("start GenEx ");
			
			try {
				List<TrainingDocument> ltd = null;
				
				ltd = TrainingDocument.findTrainingDocumentByUserAndDomain(task.getUserDocumentDomain().getDomainUser(), task.getUserDocumentDomain().getDocumentDomain());
				
				System.out.println("td size = " + ltd.size() + ".");
				
				BaseAlgo algo = new GenEx();
				algo.train(ltd);
				
				//clear used training documents
				for (TrainingDocument td : ltd) {
//					td.remove();
				}
				
//				System.exit(1);
			} catch (Exception ex) {
				ex.printStackTrace(System.out);
			}
			
			System.out.println("finish GenEx ");
			task.setFinishTime(new Date());
			task.setCurrentState(DocumentDomainTask.DomainTaskEnumState.FINISHED);
			System.out.println("finish GenEx 2");
			try{
				task = task.merge();
			}catch(Exception ex){
				System.out.println(" merge etc 2");
				ex.printStackTrace();
			}
			System.out.println("finish GenEx 3");
			
			//TODO implement translate task 
//			//Auto create translation task for non-English tasks
//			CustomLocale locale = task.getDocument().getCustomLocale();
//			System.out.println("finish GenEx 4");
//			if(locale.getShort_language().toLowerCase().contains("en") == false){
//				TranslateTask transl = new TranslateTask();
//				transl.setDocument(task.getDocument());
//				transl.setCurrentState(TranslateTask.EnumState.NOT_STARTED);
//				transl.setEnabled(true);
//				CustomLocale translateTo;
//				try{
//					translateTo = CustomLocale.findCustomLocalesByLanguage_full(
//									"English").setFirstResult(0).setMaxResults(1).getSingleResult();
//				}catch(EmptyResultDataAccessException ex){
//					translateTo = new CustomLocale();
//					translateTo.setShort_language("en");
//					translateTo.setShort_country("us");
//					translateTo.setCountry_full("United States");
//					translateTo.setLanguage_full("English");
//					translateTo.persist();
//				}
//				transl.setLocale(translateTo);
//				transl.persist();
//			}
			//Info
			System.out.println("Exec time for domain task is: "
					+ ((task.getStartTime().getTime() - task.getFinishTime()
							.getTime()) / 1000) + "secs");
		}else{
			System.out.println("No tasks found.");
		}
	 
	}
}

	